﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using shradhabookstores;
using PagedList;

namespace shradhabookstores.Controllers
{
    public class AccountAdminController : Controller
    {
        private shradhabookstoresEntities5 db = new shradhabookstoresEntities5();

        // GET: /AccountAdmin/
        public ActionResult Index(string sortOrder, string currentFilter, string SearchString, int? page)
        {
            if (Session["UserName"] == null)
            {
                return RedirectToAction("Login", "AccountAdmin");
            }

            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
          //   ViewBag.LevelSortParm = sortOrder == "Level_" ? "Level_desc" : "Level_";
            if (SearchString != null)
            {
                page = 1;
            }
            else
            {
                SearchString = currentFilter;
            }

            ViewBag.CurrentFilter = SearchString;

            var user = from u in db.Admins
                       select u;
            if (!String.IsNullOrEmpty(SearchString))
            {
                user = user.Where(u => u.UserName.ToUpper().Contains(SearchString.ToUpper())
                  //      || u.Level_.ToUpper().Contains(SearchString.ToUpper())
                    );
            }
            switch (sortOrder)
            {
                case "Name_desc":
                    user = user.OrderByDescending(u => u.UserName);
                    break;
                //case "Level_":
                //    user = user.OrderByDescending(u => u.Level_);
                //    break;
                default:
                    user = user.OrderBy(u => u.UserName);
                    break;

            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(user.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult Login(string returnUrl, string UserName, string Password)
        {
            ViewBag.message = "";
            if (UserName != null && Password != null)
            {
                var user = (from p in db.Admins
                            where p.UserName == UserName && p.Password == Password
                            select p).FirstOrDefault();

                if (user == null)
                {
                    ViewBag.message = "Dang nhap loi";
                }
                else
                {
                    Session["UserName"] = UserName;
                    Session["ID"] = user.ID;
                    Session["Level"] = user.Level_;
                    return RedirectToAction("Index", "Home");
                }
            }
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }
        public ActionResult LogOff()
        {
            Session.Remove("UserName");
            Session.Remove("ID");
            Session.Remove("Level");
            return RedirectToAction("Login");
        }
        // GET: /AccountAdmin/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Admin admin = db.Admins.Find(id);
            if (admin == null)
            {
                return HttpNotFound();
            }
            return View(admin);
        }

        // GET: /AccountAdmin/Create
        public ActionResult Create()
        {
            ViewBag.Level_ = new SelectList(db.Level_, "ID", "Level_1");
            return View();
        }

        // POST: /AccountAdmin/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,UserName,Password,Level_")] Admin admin)
        {
            if (ModelState.IsValid)
            {
                db.Admins.Add(admin);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Level_ = new SelectList(db.Level_, "ID", "Level_1", admin.Level_);
            return View(admin);
        }

        // GET: /AccountAdmin/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Admin admin = db.Admins.Find(id);
            if (admin == null)
            {
                return HttpNotFound();
            }
            ViewBag.Level_ = new SelectList(db.Level_, "ID", "Level_1", admin.Level_);
            return View(admin);
        }

        // POST: /AccountAdmin/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,UserName,Password,Level_")] Admin admin)
        {
            if (ModelState.IsValid)
            {
                db.Entry(admin).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Level_ = new SelectList(db.Level_, "ID", "Level_1", admin.Level_);
            return View(admin);
        }

        // GET: /AccountAdmin/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Admin admin = db.Admins.Find(id);
            if (admin == null)
            {
                return HttpNotFound();
            }
            return View(admin);
        }

        // POST: /AccountAdmin/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Admin admin = db.Admins.Find(id);
            db.Admins.Remove(admin);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
